/*
 * @Author: tianchunyu
 * @Date: 2020-05-11 18:01:15
 * @LastEditTime: 2020-05-11 18:21:34
 * @LastEditors: Please set LastEditors
 * @Description:Lifting  drive,the drive control both motor which can lift wash institutions and drop it.
 *  * @FilePath: \X\app\xp\driver\lifter_s.h
 */
#ifndef _LIFTER_S_H
#define _LIFTER_S_H
#include "synron_ls_modbus.h"
#include<stdlib.h>
#include <math.h>
/*
本驱动特别注意坐标关系，本驱动绝对坐标为整个机器上电后 电缸再最底下的位置 此时为0坐标 ，
电缸向上运动，编码器值为正，所下发的绝对脉冲指令也是相对于整个坐标而言，绝对坐标！！！
如下图：
Y+ ^
   |
   |
   |
   |
   |
   |
   |
 0 |______> 0
 Y坐标值范围0~1400 mm 

*/

#define LIFTER_line_speed1 0.05  //升降缸移动速度,m/s
/*
电机脉冲数坐标X          (0,+) 所发脉冲数的绝对位置
电缸位置坐标Y           (0,+) mm
X = 3000 * Y  
Y =(1 / 3000)* X
*/
#define COR_DISY_TO_COR_PULS (3000.0) // 电缸实际位置到所发脉冲数坐标的实际比值
#define COR_PULS_TO_COR_DIS  (1.0/3000.0) //电缸实际脉冲数坐标到实际距离坐标的比值 
#define COR_UPDOWNSPEED_TO_MPTORPRM (18000.00)// x*1000*1.5*60/5

#define LIFTER_ratio 1.5    //电缸减速比
#define LIFTER_pitch 5      //电缸螺距mm
#define LIFTER_part 10000   //一圈脉冲数     一圈编码器的值这个是固定的
#define LIFTER_SET_ZERO  1000 // unuit mm   规定的0 位 程序里的0位置是初始状态，电缸再最底下的时候
#define LIFTER_MAX_CORDINATE_Y_VALUE  3000  ///电缸最高可以升的高度 绝对位置坐标最高 暂定 mm
#define SAFE_HEIGHT     (1385)//坐标范围值是0~1400  安全范围设置为1385


/*
input and  output singal defination 
*/

#define L_BOARD_ID         (1)   
#define L_INPUT_PIN_A      (13-1)   //向左电机输入信号引脚 急停
#define L_INPUT_PIN_B      (14-1)   //向右电机输入信号引脚 急停
#define L_OUT_PIN_A        (3-1)    //驱动器的输出信号io子板输入 对应A
#define L_OUT_PIN_B        (4-1)    //驱动器的输出信号io子板输入 对应B
#define L_HIGH_STATE       (1)   //输入信号高电平状态
#define L_LOW_STATE        (0)    // 输入信号低电平状态
#define L_EMG_POWEROFF     (18-1)   //  驱动器电源io开关引脚
#define LOW_LIMIT_POSITION  (2-1)       //下限位IO
#define ZERO_LIMIT_POSITION (1-1)       //上限位 零点IO 






#define LIFTER_motor 1  //升降缸控制电机数，1=从动，2=双控

#define LIFTER_io_up    0   //磁力顶部 io pin number
#define LIFTER_io_down  1   //磁力底部 io pin number
#define LIFTER_io_touch 0   //磁力传感器触发状态

#define LIFTER_io_origin 2  //升降缸原点位置 io pin number
#define LIFTER_io_bottom 3  //升降缸底部位置 io pin number


#define ENCODE_ERROR_PULSE (0x7FFFFFFF)     //编码器值错误值定义  不可能达到最大值
//lefter motor class
typedef struct{
    u8 init;    //init state
    u8 check_zero;  //check zero state
    u8 run; //      lifter run state
    u8 res; //保留
    double coord_A_Y;   //move location 绝对坐标位置 最高 1400mm 暂定
    double coord_B_Y;   //move location 绝对坐标位置 最高 1400mm 暂定
    double coord_Y;   //move location 绝对坐标位置 最高 1400mm 暂定
    int origin_encode;  //origin encode
    int origin_bottom;  //bottom encode
    s32 encode_A_motor0;//A电机的编码器值  注意掉电丢失 只针对上一次的记录的值 驱动器掉电后重启可以参考
    s32 encode_B_motor1;//B 电机的编码器值
    SYNRO_handler *motor[2];  //lifer motor
}LIFTER_class;



extern LIFTER_class lifter;
extern void xp_lifter_config(void);
extern int xp_lifter_init(u8 port, int speed);
extern int xp_lifter_move_s(LIFTER_class * lifterp, double coordinate_y, double runspeed);
extern int xp_lifter_move_t(LIFTER_class*lifterp,double coordinate_y,u16 time_ms);
extern int xp_lifter_stop(LIFTER_class * lifterp);
extern int xp_lifter_debug(char * type, char * fun, char * param);
extern int xp_lifter_check_zero(void);
extern int xp_lifter_get_abs_pulse(double coordinate_y);
extern int xp_lifter_get_locate(LIFTER_class * lifterp);
extern int xp_lifter_move_s_a(LIFTER_class*lifterp,double coordinate_y,double runspeed);
extern int xp_lifter_move_s_b(LIFTER_class*lifterp,double coordinate_y,double runspeed);
extern int xp_lifter_get_locate_a(LIFTER_class*lifterp);
extern int xp_lifter_get_locate_b(LIFTER_class*lifterp);
extern void xp_lifter_error_anction(void);
extern int xp_lifter_read_inputsig(void);
extern int xp_lifter_move_abs(LIFTER_class*lifterp,double coordinate_y,double runspeed);
extern int xp_lifter_accdcctime_set(LIFTER_class*lifterp,u16 times); 
extern int xp_lifter_find_low_limit_position(LIFTER_class * lifterp);
extern int xp_lifter_find_zero_position(LIFTER_class * lifterp);


#endif
